<script setup lang="ts">
import { deleteArticleCategoryService } from '@/api/category';
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import { onMounted, ref } from 'vue'
import CategoryDialog from '@/components/article/CategoryDialog.vue';
import { categorys } from '@/models/article';
// 文章分类列表查询
import useCategory from '@/hooks/useCategory';
const { getCategory } = useCategory()
onMounted(() => {
  getCategory()
})
// 分类弹窗
let dialogVisible = ref(false)
// 更新弹窗状态
const updateVisible = (newValue: boolean) => {
  dialogVisible.value = newValue
}
// 子组件标题
const title = ref('')

import { categoryModel } from '@/models/article';
// 展示编辑弹窗
const showDialog = (row: any) => {
  dialogVisible.value = true
  title.value = '编辑分类'
  categoryModel.value.categoryName = row.categoryName
  categoryModel.value.categoryAlias = row.categoryAlias
  categoryModel.value.id = row.id
}
// 清空数据模型
const clearData = () => {
  categoryModel.value.categoryName = ''
  categoryModel.value.categoryAlias = ''
}
//删除分类  给删除按钮绑定事件
const deleteCategory = (row: any) => {
  ElMessageBox.confirm(
    '你确认删除该分类信息吗？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      //用户点击了确认
      const result = await deleteArticleCategoryService(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      getCategory()
    })
    .catch(() => {
      //用户点击了取消
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>文章分类</span>
        <div class="extra">
          <el-button type="primary" @click="dialogVisible = true, title = '添加分类', clearData()">添加分类</el-button>
          <CategoryDialog :title v-model="dialogVisible" :getCategory @update-dialogVisible="updateVisible" />
        </div>
      </div>
    </template>
    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="categoryName"></el-table-column>
      <el-table-column label="分类别名" prop="categoryAlias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </el-card>
</template>

<style scoped lang="scss">
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>